import * as Api from '@@/api/api';
import { message } from 'antd';
import { Dispatch } from 'redux';

/**
 * 同步 action
 */
export const SET_UPDATE_USER = 'SET_UPDATE_USER'; // 选择设置用户
export const ac_set_update_user = (user: any): ReduxActionObject => ({
  type: SET_UPDATE_USER,
  user
})

/**
 * 异步 action
 */
// 获取图片验证码
export const UUID_ASYNC: string = 'IMAGECODE_ASYNC';
export const ac_uuid_async = () => ({
  type: UUID_ASYNC,
  async payload() {
    return await Api.getImgCode();
  }
})

// 登录
export const LOGIN: string = 'LOGIN';
export const ac_login = (param: any, dispatch: Dispatch<any>) => ({
  type: LOGIN,
  payload: {
    data: { loginLoading: true, res: null },
    async promise () {
      const res = await Api.login(param);
      if (res && res.retCode !== '0') {
        dispatch(ac_uuid_async());
      }
      return res;
    }
  }
})

/* --======================会议室管理======================-- */
// 查询会议室列表
export const GET_MEETINGROOM = 'GET_MEETINGROOM';
export const ac_get_meetingRoom = () => ({
  type: GET_MEETINGROOM,
  payload: {
    data: { meetingRoomLoading: true, res: null },
    async promise() {
      return await Api.getMeetingRooms();
    }
  }
})

// 删除会议室
export const DELETE_MEETINGROOM = 'DELETE_MEETINGROOM';
export const ac_delete_meetingRoom = (param: any, dispatch: Dispatch<any>) => ({
  type: DELETE_MEETINGROOM,
  async payload () {
    const res = await Api.deleteMeetingRoom(param);
    if (res) {
      dispatch(ac_get_meetingRoom());
    }
    return res;
  }
})

// 添加会议室
export const ADD_MEETINGROOM = 'ADD_MEETINGROOM';
export const ac_add_meetingRoom = (param: any, dispatch: Dispatch<any>, setIsModalVisible: Function) => {
  const hide = message.loading('处理中');
  return {
    type: ADD_MEETINGROOM,
    async payload() {
      const res = await Api.addMettingRoom(param);
      if (res) {
        message.success("添加会议室成功")
        dispatch(ac_get_meetingRoom());
        setIsModalVisible(false);
      }
      hide();
      return res;
    }
  }
}
/* --======================会议室管理--end======================-- */

/* --======================意向商家======================-- */
// 查询意向商家列表
export const GET_BUSINESS = 'GET_BUSINESS';
export const ac_get_business = () => ({
  type: GET_BUSINESS,
  payload: {
    data: { businessLoading: true, res: null },
    async promise() {
      return await Api.getBusiness();
    }
  }
})

// 添加意向商家
export const ADD_BUSINESS = 'ADD_BUSINESS';
export const ac_add_business = (param: any, dispatch: Dispatch<any>, setIsModalVisible: Function) => {
  const hide = message.loading('处理中');
  return {
    type: ADD_BUSINESS,
    async payload () {
      const res = await Api.addBusiness(param);
      if (res) {
        message.success('添加意向商家成功');
        dispatch(ac_get_business());
        setIsModalVisible(false);
      }
      hide();
      return res;
    }
  }
}

// 删除意向商家
export const DELETE_BUSINESS = 'DELETE_BUSINESS';
export const ac_delete_business = (param: any, dispatch: Dispatch<any>) => ({
  type: DELETE_BUSINESS,
  async payload () {
    const res = await Api.deleteBusiness(param);
    if (res) {
      dispatch(ac_get_business());
    }
    return res;
  }
})

/* --======================意向商家--end======================-- */

/* --======================用户管理======================-- */
// 查询所有用户
export const GET_ALLUSERS = 'GET_ALLUSERS';
export const ac_get_allusers = () => ({
  type: GET_ALLUSERS,
  payload: {
    data: { usersLoading : false, res: [] },
    async promise () {
      const res = await Api.getAllUsers();
      if (res) { return res.message; }
      return [];
    }
  }
})

// 删除指定用户
export const DELETE_USER = 'DELETE_USER';
export const ac_delete_user = (param: any, dispatch: Dispatch<any>) => ({
  type: DELETE_USER,
  async payload () {
    const res = await Api.deleteUser(param);
    if (res) {
      message.success('删除用户成功');
      dispatch(ac_get_allusers());
    }
    return res;
  }
})

// 修改用户
export const UPDATE_USER = '';
export const ac_update_user = (param: any, dispatch: Dispatch<any>, hideModal: Function) => {
  const hide = message.loading('处理中');
  return {
    type: UPDATE_USER,
    async payload () {
      const res = await Api.updateUser(param);
      if (res) {
        message.success('修改用户信息成功');
        dispatch(ac_get_allusers());
        hideModal();
      }
      hide();
      return res;
    }
  }
}

/* --======================意向商家--end======================-- */
